// 负责用户业务逻辑处理
const adminModel = require('../models/adminModel');
// 导入uuid功能（生成随机字符串）
const { v4: uuidv4 } = require('uuid');
const Jwt = require('../common/jwt');
// 定义用户类
class User {
    // 1. 注册
    async register(req, res) {
        let admin_acc = req.body.admin_acc;
        let admin_pwd = req.body.admin_pwd;
        let admin_nick_name = req.body.admin_nick_name;
        // 处理用户信息
        let admin_id = uuidv4();
        let dt = new Date();
        let admin_create_time = dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
        if (admin_acc == undefined || admin_pwd == undefined || admin_nick_name == undefined) {
            res.send({
                code: -1,
                msg: '注册时缺少参数'
            })
        }
        else {
            await adminModel.register(admin_id, admin_acc, admin_pwd, admin_nick_name, admin_create_time)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
    }
    // 2. 登录
    async login(req, res) {
        let admin_acc = req.body.admin_acc;
        let admin_pwd = req.body.admin_pwd;
        if (admin_acc == undefined || admin_pwd == undefined) {
            res.send({
                code: -1,
                msg: '请填写账号或密码'
            })
        }
        else {
            adminModel.login(admin_acc, admin_pwd)
                .then(
                    // 账号和密码在此处做判断
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    // 执行sql语句出现异常
                    err => {
                        res.send(err);
                    }
                )
        }
    }
    // 3. 注销
    async remove(req, res, next) {
        //获取用户临时身份
        let token = req.body.token || req.headers.token;
        //  let token = req.body.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // console.log(result);
        if (result.data) {
            // 记录用户admin_id
            let admin_id = result.data;
            // 登录状态
            await adminModel.remove(admin_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err);
                    }
                )
        }
        else {
            // 未登录状态
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }

        next();
    }
    // 4. 获取用户头像
    async getPicUrl(req, res) {
        let token = req.query.token || req.headers.token;
        // let token = req.query.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // console.log("2.0",result);
        // 判断用户id是否值
        if (result.data) {
            let admin_id = result.data;
            // 获取真实用户头像
            await adminModel.getPicUrl(admin_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }

    }

    // 5. 设置用户头像
    async setPicUrl(req, res) {
        let admin_picUrl = req.body.admin_picUrl;
        // let token = req.headers.token || req.headers.token;
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // console.log(result);
        if (result.data) {
            // 获取用户身份
            let admin_id = result.data;
            // 设置用户头像
            adminModel.setPicUrl(admin_id, admin_picUrl).then(
                data => {
                    res.send(data);
                }
            )
                .catch(
                    err => {
                        res.send(err);
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }
    }

    // 6. 找回密码
    async findPwd(req, res) {
        let admin_acc = req.body.admin_acc;
        let new_admin_pwd = req.body.new_admin_pwd;//新密码
        await adminModel.findPwd(admin_acc, new_admin_pwd)
            .then(
                content => {
                    res.send(content)
                }
            )
            .catch(
                err => {
                    res.send(err);
                }
            )
    }

    // 7. 用户列表
    async userList(req, res) {
        //获取用户临时身份
        let token = req.query.token || req.headers.token;
        // console.log(token);
        //  let token = req.body.token;
        // let jwt = new Jwt(token);
        // 判断token是否在有效期
        // let result = jwt.verifyToken();
        // console.log(result);
        if (token) {
            // 接收get请求方式的参数 判断page和pageSize是否有值，有值就把字符串转数字
            let page = req.query.page && parseInt(req.query.page)
            let pageSize = req.query.pageSize && parseInt(req.query.pageSize)
            await adminModel.userList(page, pageSize)
                .then(
                    content => {
                        res.send(content);
                    },
                    error => {
                        res.send(error);
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号。',
                res
            })
        }
    }

    // 8. 用户列表
    async deleteUser(req, res) {
        //获取用户临时身份
        let token = req.body.token || req.headers.token;
        //  let token = req.body.token;
        // let jwt = new Jwt(token);
        // 判断token是否在有效期
        // let result = jwt.verifyToken();
        if (token) {
            // 接收post请求方式的参数 
            let user_id = req.body.user_id;
            await adminModel.deleteUser(user_id)
                .then(
                    content => {
                        res.send(content);
                    },
                    error => {
                        res.send(error);
                    }
                )
        }
    }

    // 统计数据
    async count(req, res) {
        let { user_sex } = req.query;
        await adminModel.count(user_sex)
        .then(
            content=>{
                res.send(content)
            }
            ,
            err=>{
                res.send(err)
            }
        )
    }
}
// 暴露用户类的实例
// 把用户对象挂载到系统对象
module.exports = new User();